/**
 * 语音控制管理器模块
 * 负责语音识别和控制
 * TODO: 这里需要大修，完全没有实现
 */

class VoiceManager {
    constructor() {
        this.isVoiceListening = false;
        this.recognition = null;
        this.voiceBtn = document.getElementById('voiceBtn');
        this.voiceIndicator = document.getElementById('voiceIndicator');
        this.init();
    }

    init() {
        this.bindEvents();
        this.initSpeechRecognition();
    }

    bindEvents() {
        if (this.voiceBtn) {
            this.voiceBtn.addEventListener('click', () => this.toggleVoiceControl());
        }
    }

    initSpeechRecognition() {
        if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
            const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
            this.recognition = new SpeechRecognition();
            
            this.recognition.lang = 'zh-CN';
            this.recognition.continuous = false;
            this.recognition.interimResults = false;
            
            this.setupRecognitionEvents();
        }
    }

    setupRecognitionEvents() {
        if (!this.recognition) return;

        this.recognition.onstart = () => {
            this.isVoiceListening = true;
            this.showVoiceIndicator();
            this.updateVoiceButton();
            window.logManager?.addLogEntry('info', '语音识别已启动');
        };

        this.recognition.onresult = (event) => {
            const command = event.results[0][0].transcript.toLowerCase();
            window.logManager?.addLogEntry('info', `语音指令: ${command}`);
            
            this.processVoiceCommand(command);
        };

        this.recognition.onerror = (event) => {
            window.logManager?.addLogEntry('error', `语音识别错误: ${event.error}`);
            this.stopVoiceRecognition();
        };

        this.recognition.onend = () => {
            this.stopVoiceRecognition();
        };
    }

    processVoiceCommand(command) {
        if (!window.socketManager) return;

        if (command.includes('开始') || command.includes('启动')) {
            if (!window.systemManager?.isSystemRunning) {
                window.systemManager?.startSystem();
            }
        } else if (command.includes('结束') || command.includes('停止')) {
            if (window.systemManager?.isSystemRunning) {
                window.systemManager?.stopSystem();
            }
        }

        // 发送语音指令到服务器
        window.socketManager.socket.emit('voice_command', { command: command });
    }

    toggleVoiceControl() {
        if (this.isVoiceListening) {
            this.stopVoiceRecognition();
        } else {
            this.startVoiceRecognition();
        }
    }

    startVoiceRecognition() {
        if (!this.recognition) {
            alert('您的浏览器不支持语音识别功能');
            return;
        }

        this.recognition.start();
    }

    stopVoiceRecognition() {
        this.isVoiceListening = false;
        this.hideVoiceIndicator();
        this.updateVoiceButton();
    }

    showVoiceIndicator() {
        if (this.voiceIndicator) {
            this.voiceIndicator.classList.add('active');
        }
    }

    hideVoiceIndicator() {
        if (this.voiceIndicator) {
            this.voiceIndicator.classList.remove('active');
        }
    }

    updateVoiceButton() {
        if (this.voiceBtn) {
            if (this.isVoiceListening) {
                this.voiceBtn.innerHTML = '<i class="fas fa-microphone-slash"></i> 停止语音';
            } else {
                this.voiceBtn.innerHTML = '<i class="fas fa-microphone"></i> 语音控制';
            }
        }
    }
}

// 导出到全局
window.VoiceManager = VoiceManager; 